package com.junjie.checkin.controller;

import com.junjie.checkin.exception.ResourceNotFountException;
import com.junjie.checkin.request.activity.ActivityPageRequest;
import com.junjie.checkin.request.activity.ActivityRequest;
import com.junjie.checkin.request.activity.RegistrationListPageRequest;
import com.junjie.checkin.response.*;
import com.junjie.checkin.response.admin.ActivityDetailsResponse;
import com.junjie.checkin.response.admin.ActivityResponse;
import com.junjie.checkin.response.admin.RegistrationDetailsResponse;
import com.junjie.checkin.response.admin.RegistrationListPageResponse;
import com.junjie.checkin.service.AdminActivityService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/admin/activities")
public class AdminActivityController {

    @Autowired
    private AdminActivityService adminActivityService;

    /**
     * 获取活动列表 (已完成)
     * @param  activityPageRequest 参数参数
     * @return 统一返回结果,放回活动列表
     */
    @GetMapping("")
    public Result<PageResult<ActivityResponse>> getActivities(ActivityPageRequest activityPageRequest){
        activityPageRequest.initDefaults();
        PageResult<ActivityResponse> pageResult = adminActivityService.getActivities(activityPageRequest);
        return Result.getResult(ResultStatus.OK,pageResult);
    }

    /**
     * 获取活动详情(已完成)
     * @param id 活动id
     * @return 统一返回结果,返回活动详情
     */
    @GetMapping("/{id}")
    public Result getActivityById(@PathVariable Integer id){
        ActivityDetailsResponse activityById = adminActivityService.getActivityById(id);
        return Result.getResult(ResultStatus.OK,activityById);
    }

    /**
     * 添加活动 (已完成,参数进行校验)  //todo,需要调整新增活动的状态为未开始，现增活动的状态为草稿
     * @param activityRequest 活动信息
     * @param request Http请求,为了拿到token
     * @return 统一返回结果,返回活动信息
     */
    @PostMapping("")
    public Result<ActivityResponse> addActivity(@RequestBody @Validated ActivityRequest activityRequest, HttpServletRequest request) throws ParseException {
        String token = request.getHeader("Authorization");  //获取token
        return Result.getResult(ResultStatus.OK, adminActivityService.addActivity(activityRequest,token));
    }

    /**
     * 修改活动 (已完成)
     * @param activityRequest 更新活动信息
     * @return 统一返回数据,无返回数据
     */
    @PutMapping("/{id}")
    public Result updateActivity(@PathVariable int id,@RequestBody @Validated ActivityRequest activityRequest,HttpServletRequest request) throws ParseException {
        // 获取用户token
        String token = request.getHeader("Authorization");
        adminActivityService.updateActivity(id,activityRequest,token);
        return Result.getResult(ResultStatus.OK);
    }
    /**
     * 取消活动 (已完成)
     * @param id 取消活动的Id
     * @return 统一返回数据,无返回数据
     */
    @DeleteMapping("/{id}")
    public Result deleteActivity(@PathVariable int id){
        adminActivityService.cancelActivity(id);
        return  Result.getResult(ResultStatus.OK);
    }

    /**
     * 获取活动报名详情
     * @param id
     * @return
     */
    @GetMapping("/registration/{id}")
    public Result<RegistrationDetailsResponse> getRegistrationDetails(@PathVariable Integer id){
        RegistrationDetailsResponse registrationDetails = adminActivityService.getRegistrationDetails(id);
        return Result.getResult(ResultStatus.OK,registrationDetails);
    }

    /**
     * 获取活动报名列表
     * @param request
     * @return
     */
    @GetMapping("/registration")
    public Result<PageResult<RegistrationListPageResponse> > getRegistrationList(RegistrationListPageRequest request){
        PageResult<RegistrationListPageResponse> pageResult = adminActivityService.getRegistrationList(request);
        return Result.getResult(ResultStatus.OK,pageResult);
    }
    /**
     * 导出报名学生数据报表
     * @param response
     */
    @GetMapping("/export/{id}")
    public void export(@PathVariable Integer id, HttpServletResponse response){
        adminActivityService.exportBusinessData(id,response);
    }

    /**
     * 批量签到
     * @param ids
     */
    @PutMapping("/batchCheckIn")
    public Result batchCheckIn(@RequestParam("ids") List<Long> ids,@RequestParam("activityId") Long activityId){
        if(ids.isEmpty()){
            throw new ResourceNotFountException();
        }
        adminActivityService.batchCheckIn(ids,activityId);
        return Result.getResult(ResultStatus.OK);
    }

    /**
     * 手动签到
     * @param id
     * @return
     */
    @PutMapping("/manualCheckIn/{id}")
    public Result manualCheckIn(@PathVariable Integer id){
        if(id == null){
            throw new ResourceNotFountException();
        }
        adminActivityService.manualCheckIn(id);
        return Result.getResult(ResultStatus.OK);
    }

    /**
     * 取消报名
     * @param id
     * @return
     */
    @PutMapping("/cancelRegistration/{id}")
    public Result cancelRegistration(@PathVariable Integer id){
        if(id == null) {
            throw new ResourceNotFountException();
        }
        adminActivityService.cancelRegistration(id);
        return Result.getResult(ResultStatus.OK);
    }
}
